﻿#region

using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            var user = new User
            {
                Username = txtUsername.Text,
                Password = txtPassword.Text
            };
            if (UserBLL.LoginUser(user))
            {
                if (!UserBLL.IsBannedUser(user))
                {
                    user = UserBLL.GetUserByUsername(user.Username);
                    if (user.Type.Equals("Customer"))
                    {
                        var customer = new Customer
                        {
                            Username = user.Username
                        };

                        CustomerBLL.GetCustomerId(customer);
                        if (!Roles.RoleExists("Customer"))
                        {
                            Roles.CreateRole("Customer");
                        }
                        if (!Roles.IsUserInRole(customer.CustomerId.ToString(), "Customer"))
                        {
                            Roles.AddUserToRole(customer.CustomerId.ToString(), "Customer");
                        }
                        FormsAuthentication.SetAuthCookie(customer.CustomerId.ToString(), chkRemember.Checked);
                        Response.Redirect("CustomerPanel/Default.aspx");
                    }
                    else
                    {
                        lblStatus.Text = "Invalid combination of Username & Password";
                        lblStatus.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblStatus.Text = "Sorry your account is banned";
                    lblStatus.ForeColor = Color.Red;
                }
            }
            else
            {
                lblStatus.Text = "Invalid combination of Username & Password";
                lblStatus.ForeColor = Color.Red;
            }
        }
    }
}